package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jstyle.jclifexd.R;

import java.text.NumberFormat;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuge on 2017/2/25 0025.
 */

public class TempHistoryDetailAdapter extends RecyclerView.Adapter {
    private static final String TAG = "MusicAdapter";
    int[] tempDetails;
    float totalCount;
    private boolean isNoData = true;
    NumberFormat numberFormat;

    public TempHistoryDetailAdapter() {
        numberFormat = NumberFormat.getPercentInstance(Locale.ENGLISH);
        numberFormat.setMaximumFractionDigits(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_temp_detail, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        String tempCount = "--";
        String tempPercent = "--";
        if (tempDetails != null) {
            int tempDetail = tempDetails[position];
            tempCount = String.valueOf(tempDetail);
            if (totalCount != 0) {
                tempPercent=numberFormat.format(tempDetail/totalCount);
            }
        }
        final ViewHolder viewHolder = (ViewHolder) holder;
        Context context = holder.itemView.getContext();
        String[] arrayTitle = context.getResources().getStringArray(R.array.temp_detailArray);
        viewHolder.tvSleepDetialTitle.setText(arrayTitle[position]);
        viewHolder.tvSleepDetialContent.setText(tempCount);
        viewHolder.tvSleepDetialLimit.setText(tempPercent);

    }


    public void setData(int[] tempCount) {
        this.tempDetails = tempCount;
        totalCount=0;
        for (int count : tempCount) {
            totalCount += count;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return 3;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sleepDetial_title)
        TextView tvSleepDetialTitle;
        @BindView(R.id.tv_sleepDetial_content)
        TextView tvSleepDetialContent;
        @BindView(R.id.tv_sleepDetial_limit)
        TextView tvSleepDetialLimit;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
